<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Course;
use app\admin\model\Filess;
use app\admin\model\News;
use app\admin\model\Newstype;
use app\admin\model\Slideshow;
use app\admin\model\Video;
use think\Request;
use think\File;
class  Files extends Base
{
    /*
     * 文件列表
     */
    public function lists(Request $request){

        $user = $this->user_info();
        $page = $request->get('page');
        //查询所有的资讯分类，
        $resource = collection(Course::order("sort asc")->select())->toArray();
        //给查询每个分类下的所有资讯
        foreach($resource as $k => $v){
            $newslist = Filess::where(array("type"=>$v['id']))->order("id desc")->paginate(10,false,[
                'path'=> "/admin/Videos/lists/typeid/".$v['id'],
                'var_page' => 'page'.$v['id'],
                'page' =>$page,
            ]);
            $resource[$k]['newslist'] = $newslist;
            $json = json_encode($newslist);
            $res = json_decode($json,true);
            $resource[$k]['page']['total'] = $res['total'];
            $resource[$k]['page']['current_page'] = $res['current_page'];
            $resource[$k]['page']['per_page'] = $res['per_page'];
        }
        $typeid = !empty($request->route("typeid")) ? $request->route("typeid") : $resource[0]['id'];
        $data = array(
            "setting" => $this->getAllSetting(),
            "activeid"=>$typeid,
            "list"=>$resource,
        );
        return view("lists",$data);
    }
    //上传文件
//    public function upload(Request $request){
//        //接收上传的文件
//        $file = $this->request->file('file');
//        if(!empty($file)){
//            //图片存的路径
//            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'news';
//            // 移动到框架应用根目录/public/uploads/ 目录下
//            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,gif,png,jpeg,txt,doc,word,excel,pdf,ppt,xls,mp4'])->rule('uniqid')->move($imgUrl);
//            $error = $file->getError();
//            //验证文件后缀后大小
//            if(!empty($error)){
//                dump($error);exit;
//            }
//            if($info){
//                // 成功上传后 获取上传信息
//
//                //获取图片的名字
//                $imgName = $info->getFilename();
//                //获取图片的路径
//                $photo= $imgName;
//
//            }else{
//                // 上传失败获取错误信息
//                $file->getError();
//            }
//        }else{
//            $photo = '';
//        }
//        if($photo !== ''){
//            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
//        }else{
//            return ['code'=>404,'msg'=>'失败'];
//        }
//    }
    // 文件上传提交
    public function upload(){
        $datas = array(
            "title" => request()->post("title"),
            "type" => request()->post("type"),
            "url" => request()->post("files"),
            "create_time"=>time(),
            "update_time"=>time(),
        );
        $res = Filess::insert($datas);
        if($res){
            $this->success("添加成功","Files/addform");
        }else{
            $this->error("添加失败","Files/addform");
        }

    }
    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => Course::select(),
        );
        return view("addform",$data);
    }


    //修改
    public function update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => Course::select(),
            "data" => Filess::where(array("id"=>$id))->find(),
        );
        return view("update",$data);
    }
    public function toUpdate(Request $request){

            $datas = array(

                "id" => request()->post("id"),
                "title" => request()->post("title"),
                "type" => request()->post("type"),
                "url" => request()->post("files"),
                "update_time"=>time(),
            );

        $res = Filess::update($datas);
        if($res){
            $this->success("修改成功","Files/lists");
        }else{
            $this->error("修改失败");
        }
    }
    //删除
    public function deletes(Request $request){
        $user = $this->user_info();
        if(Filess::destroy($request->post('id'))){
            $this->success("删除成功","Files/lists?typeid=".$request->post('typeid'));
        }else{
            $this->error("删除失败");
        }
    }
    //------------------------------------------------------------------------------------------------------------------
    /*
            *上传文件
            */
    public function upload_file(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');

        if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'file';
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['size'=>838860800,'ext'=>'txt,doc,xls,ppt,docx,xlsx,pptx'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                dump($error);exit;
            }
            if($info){
                //获取图片的名字
                $imgName = $info->getFilename();
                //获取图片的路径
                $photo= $imgName;
            }else{
                // 上传失败获取错误信息
                $file->getError();
            }
        }else{
            $photo = '';
        }
        if($photo !== ''){
            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }
    }

}
